package com.docHunt.action;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.docHunt.BO.UserBO;
import com.docHunt.DTO.UserDTO;

public class DoctorRegAction extends BaseAction {

	@Autowired
	UserBO userBO;

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(DoctorRegAction.class);

	public UserDTO userDTO;
	public String confirmPassword;

	public String init() throws Exception {

		logger.info("DoctorRegAction :: init - method start");

		return SUCCESS;
	}

	public String addUser() throws Exception {

		logger.info("DoctorRegAction :: addUser - method start");

		userBO.addUser(userDTO);

		addActionMessage(addErrorOrMsg("msg.check.verificationcode",new Object[]{}));
		
		logger.info("DoctorRegAction :: addUser - method end");
		
		return SUCCESS;

	}

	public void validate() {

		if (userDTO.getLoginId() == null || userDTO.getLoginId().equals("")) {
			addActionError(addErrorOrMsg("error.required",new Object[]{getText("docHunt.doctorReg.email")}));
		}

		if (userDTO.getPassword() == null || userDTO.getPassword().equals("")) {
			addActionError(addErrorOrMsg("error.required",new Object[]{getText("docHunt.doctorReg.password")}));
		}

		if (confirmPassword == null || confirmPassword.equals("")) {
			addActionError(addErrorOrMsg("error.required",new Object[]{getText("docHunt.doctorReg.confirmPassword")}));
		}

		if (!userDTO.getPassword().equals(confirmPassword)) {
			addActionError(addErrorOrMsg("error.notMatched",new Object[]{getText("docHunt.doctorReg.password"),
					getText("docHunt.doctorReg.confirmPassword")}));
		}
		
		if(userBO.getUser(userDTO.getLoginId()) != null){
			addActionError(addErrorOrMsg("error.user.exists",new Object[]{userDTO.getLoginId()}));
		}
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

}